#include "qclockwidget.h"

#include "../../libs/shared/qhostfactory.h"
#include "../../libs/qwt/qwt_analog_clock.h"
#include "../../libs/qwt/qwt_dial_needle.h"
#include "../../libs/shared/property/qenumproperty.h"
#include "../../libs/shared/property/qintproperty.h"
#include "../../libs/shared/property/qboolproperty.h"
#include "../../libs/shared/property/qcolorproperty.h"
#include "../../libs/shared/qpropertyfactory.h"


int needle_info_id=qRegisterMetaType<tagNeedleInfo>("NeedleInfo");

QClockNeedleProperty::QClockNeedleProperty(QAbstractProperty *parent):
    QAbstractProperty(parent)
{
    set_property("type","Clock_Needle");

    m_style=new QEnumProperty(this);
    m_style->set_attribute("show_name",tr("Style"));
    m_style->set_property("name","Style");

    tagComboItem item;
    ComboItems  items;

    item.m_text="Arrow";
    item.m_value=QwtDialSimpleNeedle::Arrow;
    items.append(item);

    item.m_text="Ray";
    item.m_value=QwtDialSimpleNeedle::Ray;
    items.append(item);

    m_style->set_attribute("items",QVariant::fromValue<ComboItems>(items));


    m_width=new QIntProperty(this);
    m_width->set_attribute("show_name",tr("Width"));
    m_width->set_property("name","width");

    m_color=new QColorProperty(this);
    m_color->set_attribute("show_name",tr("Color"));
    m_color->set_property("name","color");
    m_color->setProperty("color",true);

    m_knob_color=new QColorProperty(this);
    m_knob_color->set_attribute("show_name",tr("Knob Color"));
    m_knob_color->set_property("name","knob_color");
    m_knob_color->setProperty("color",true);

    m_knob_visible=new QBoolProperty(this);
    m_knob_visible->set_attribute("show_name",tr("Knob Visible"));
    m_knob_visible->set_property("name","knob_visible");
}

void QClockNeedleProperty::set_value(const QVariant &value)
{
    tagNeedleInfo info=value.value<tagNeedleInfo>();

    m_style->set_value(info.m_style);
    m_width->set_value(info.m_width);
    m_color->set_value(QVariant::fromValue<QGradient>(info.m_color));
    m_knob_color->set_value(QVariant::fromValue<QGradient>(info.m_knob_color));
    m_knob_visible->set_value(info.m_knob_visible);
    QAbstractProperty::set_value(value);
}

QString QClockNeedleProperty::get_value_text()
{
    return QString("%1[%2]")
            .arg(m_style->get_value_text())
            .arg(m_width->get_value().toInt());
}

QIcon QClockNeedleProperty::get_value_icon()
{
    return QIcon();
}

QString QClockNeedleProperty::get_property_name()
{
    return "Clock_Needle";
}

void QClockNeedleProperty::child_value_changed(const QVariant &, const QVariant &)
{
    tagNeedleInfo info=get_value().value<tagNeedleInfo>();
    info.m_style=(QwtDialSimpleNeedle::Style)m_style->get_value().toInt();
    info.m_width=m_width->get_value().toDouble();
    info.m_color=m_color->get_value().value<QGradient>();
    info.m_knob_color=m_knob_color->get_value().value<QGradient>();
    info.m_knob_visible=m_knob_visible->get_value().toBool();
    QAbstractProperty::set_value(QVariant::fromValue<tagNeedleInfo>(info));
}

void QClockNeedleProperty::make_value()
{
    tagNeedleInfo info=get_value().value<tagNeedleInfo>();
    info.m_style=(QwtDialSimpleNeedle::Style)m_style->get_value().toInt();
    info.m_width=m_width->get_value().toDouble();
    info.m_color=m_color->get_value().value<QGradient>();
    info.m_knob_color=m_knob_color->get_value().value<QGradient>();
    info.m_knob_visible=m_knob_visible->get_value().toBool();
    m_value.setValue<tagNeedleInfo>(info);
}

void QClockNeedleProperty::write_value()
{

}

bool QClockNeedleProperty::cmp(const QVariant &value)
{
    tagNeedleInfo info1=get_value().value<tagNeedleInfo>();
    tagNeedleInfo info2=value.value<tagNeedleInfo>();
    return info1==info2;
}

QClockWidget::QClockWidget(QWidget *parent):
    QwtAnalogClock(parent)
{
    QTimer *timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(setCurrentTime()));
    timer->start(300);
    setCurrentTime();
    setMinimumSize(100,100);

    QwtDialSimpleNeedle *n=(QwtDialSimpleNeedle*)d_hand[HourHand];
    m_hour_needle.m_color=QLinearGradient();
    m_hour_needle.m_color.setColorAt(0,n->palette().color(QPalette::Mid));
    m_hour_needle.m_width=n->width();
    m_hour_needle.m_style=n->style();
    m_hour_needle.m_knob_color.setColorAt(0,n->palette().color(QPalette::Base));
    m_hour_needle.m_knob_visible=n->hasKnob();

    n=(QwtDialSimpleNeedle*)d_hand[MinuteHand];
    m_min_needle.m_color=QLinearGradient();
    m_min_needle.m_color.setColorAt(0,n->palette().color(QPalette::Mid));
    m_min_needle.m_width=n->width();
    m_min_needle.m_style=n->style();
    m_min_needle.m_knob_color.setColorAt(0,n->palette().color(QPalette::Base));
    m_min_needle.m_knob_visible=n->hasKnob();

    n=(QwtDialSimpleNeedle*)d_hand[SecondHand];
    m_second_needle.m_color=QLinearGradient();
    m_second_needle.m_color.setColorAt(0,n->palette().color(QPalette::Mid));
    m_second_needle.m_width=n->width();
    m_second_needle.m_style=n->style();
    m_second_needle.m_knob_color.setColorAt(0,n->palette().color(QPalette::Base));
    m_second_needle.m_knob_visible=n->hasKnob();
}

tagNeedleInfo QClockWidget::hourNeedle()
{
    return m_hour_needle;
}

void QClockWidget::setHourNeedle(const tagNeedleInfo &info)
{
    if(info.m_width<0.01)
    {
        return;
    }

    if(m_hour_needle!=info)
    {
        m_hour_needle=info;
        QwtDialSimpleNeedle *needle=(QwtDialSimpleNeedle*)d_hand[QwtAnalogClock::HourHand];
        needle->setWidth(info.m_width);
        needle->setStyle((QwtDialSimpleNeedle::Style)info.m_style);
        QPalette p=needle->palette();
        p.setColor(QPalette::Mid,info.m_color.stops().first().second);
        p.setColor(QPalette::Base,info.m_knob_color.stops().first().second);
        needle->setPalette(p);
        needle->setHasKnob(info.m_knob_visible);
        update();
    }
}

void QClockWidget::setMinNeedle(const tagNeedleInfo &info)
{
    if(info.m_width<0.01)
    {
        return;
    }

    if(m_min_needle!=info)
    {
        m_min_needle=info;
        QwtDialSimpleNeedle *needle=(QwtDialSimpleNeedle*)d_hand[QwtAnalogClock::MinuteHand];
        needle->setWidth(info.m_width);
        needle->setStyle((QwtDialSimpleNeedle::Style)info.m_style);
        QPalette p=needle->palette();
        p.setColor(QPalette::Mid,info.m_color.stops().first().second);
        p.setColor(QPalette::Base,info.m_knob_color.stops().first().second);
        needle->setPalette(p);
        needle->setHasKnob(info.m_knob_visible);
        update();
    }
}

tagNeedleInfo QClockWidget::minNeedle()
{
    return m_min_needle;
}

void QClockWidget::setSecondNeedle(const tagNeedleInfo &info)
{
    if(info.m_width<0.01)
    {
        return;
    }

    if(m_second_needle!=info)
    {
        m_second_needle=info;
        QwtDialSimpleNeedle *needle=(QwtDialSimpleNeedle*)d_hand[QwtAnalogClock::SecondHand];
        needle->setWidth(info.m_width);
        needle->setStyle((QwtDialSimpleNeedle::Style)info.m_style);
        QPalette p=needle->palette();
        p.setColor(QPalette::Mid,info.m_color.stops().first().second);
        p.setColor(QPalette::Base,info.m_knob_color.stops().first().second);
        needle->setPalette(p);
        needle->setHasKnob(info.m_knob_visible);
        update();
    }
}


tagNeedleInfo QClockWidget::secondNeedle()
{
    return m_second_needle;
}


QClockWidgetHost::QClockWidgetHost(QAbstractHost *parent):
    QwtDialHost(parent)
{
}

void QClockWidgetHost::create_object()
{
    m_object=new QClockWidget;
    m_object->setObjectName("clock");
}

QString QClockWidgetHost::get_show_name()
{
    return tr("Clock");
}

QString QClockWidgetHost::get_show_group()
{
    return tr("QWT Widgets");
}

QString QClockWidgetHost::get_show_icon()
{
    return ":/images/clock.png";
}

void QClockWidgetHost::init_property()
{
    QwtDialHost::init_property();

    remove_property("value");
    remove_property("totalSteps");
    remove_property("singleSteps");
    remove_property("pageSteps");
    remove_property("stepAlignment");
    remove_property("readOnly");
    remove_property("tracking");
    remove_property("wrapping");
    remove_property("invertedControls");
    remove_property("mode");
    remove_property("minScaleArc");
    remove_property("maxScaleArc");
    remove_property("lowerBound");
    remove_property("upperBound");
    remove_property("scaleMaxMajor");
    remove_property("scaleMaxMinor");
    remove_property("scaleStepSize");
    remove_property("needle_info");

    QAbstractProperty* pro;

    pro=QPropertyFactory::create_property("Clock_Needle");
    if(pro!=NULL)
    {
        pro->set_property("name","hour_needle");
        pro->set_attribute("show_name",tr("Hour Needle"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Clock_Needle");
    if(pro!=NULL)
    {
        pro->set_property("name","min_needle");
        pro->set_attribute("show_name",tr("Minute Needle"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }


    pro=QPropertyFactory::create_property("Clock_Needle");
    if(pro!=NULL)
    {
        pro->set_property("name","second_needle");
        pro->set_attribute("show_name",tr("Second Needle"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    set_property_value("geometry",QRect(0,0,200,200));
}

qRegisterHost("qwt.clock",QClockWidgetHost,6000)
qRegisterProperty(QClockNeedleProperty)
